package com.bhs.sansonglogistics.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.Location;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.ui.order.SourceGoodsDetailsActivity;

/**
 * 弹窗辅助类
 *
 * @ClassName WindowUtils
 */
public class WindowUtils {
    private final String LOG_TAG = "WindowUtils";
    private View mView = null;
    private WindowManager mWindowManager = null;
    private Activity mContext = null;
    public Boolean isShown = false;
    private View view;
    private TextView mTvOrderType;
    private TextView mTvPlaceDeparture;
    private TextView mTvDistance;
    private TextView mTvArrivalPoint;
    private TextView mTvMoney;

    /**
     * 显示弹出框
     *
     * @param context
     */
    public void showPopupWindow(final Activity context) {
        if (isShown) {
            LogUtils.d(LOG_TAG, "return cause already shown");
            return;
        }
        isShown = true;
        LogUtils.d(LOG_TAG, "showPopupWindow");
        // 获取应用的Context
        mContext = context;
        // 获取WindowManager
        mWindowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        if (mView == null) {
            mView = setUpView(context);
        }
        final LayoutParams params = new LayoutParams();
        // 类型
        //params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;//不兼容8.0
        params.type = Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
                LayoutParams.TYPE_SYSTEM_OVERLAY :
                LayoutParams.TYPE_APPLICATION_OVERLAY;//兼容了8.0
        // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
        // 设置flag
        int flags = LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE，弹出的View收不到Back键的事件
        params.flags = flags;
        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;
        // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
        // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时，后面的应用图标由不可长按变为可长按
        // 不设置这个flag的话，home页的划屏会有问题
        params.width = LayoutParams.MATCH_PARENT;
        params.height = LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;
        mWindowManager.addView(mView, params);
        LogUtils.d(LOG_TAG, "add view");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hidePopupWindow();
            }
        }, 3000);//3秒后执行Runnable中的run方法

    }

    /**
     * 隐藏弹出框
     */
    public void hidePopupWindow() {
        LogUtils.d(LOG_TAG, "hide " + isShown + ", " + mView);
        if (isShown && null != mView) {
            LogUtils.d(LOG_TAG, "hidePopupWindow");
            mWindowManager.removeView(mView);
            isShown = false;
        }
    }

    private View setUpView(final Context context) {
        LogUtils.d(LOG_TAG, "setUp view");
        view = LayoutInflater.from(context).inflate(R.layout.floating_window_new_order,
                null);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                hidePopupWindow();
            }
        });
        initView();
        return view;
    }

    public void setData(OrderGoodsBean bean) {
        mTvOrderType.setText(bean.getOrder_type() == 1 ? "指派" : "极速");
        mTvPlaceDeparture.setText(bean.getPickup_address());
        mTvArrivalPoint.setText(bean.getReceive_address());
        mTvDistance.setText(String.format("距您%s公里", GetDistanceUtils.getDistance(Double.parseDouble(bean.getPickup_longitude())
                , Double.parseDouble(bean.getPickup_latitude()), Location.companyLon, Location.companyLat)));
        mTvMoney.setText(String.format("%s元", bean.getLast_price()));
        view.findViewById(R.id.ll_check).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mContext.startActivity(new Intent(mContext, SourceGoodsDetailsActivity.class)
                        .putExtra("deliver_sn", bean.getDeliver_sn())
                        .putExtra("type", bean.getOrder_type())
                        .putExtra("subscribe", bean.getIs_yuyue() > 0));
            }
        });
    }

    private void initView() {
        mTvOrderType = view.findViewById(R.id.tv_order_type);
        mTvPlaceDeparture = view.findViewById(R.id.tv_place_departure);
        mTvDistance = view.findViewById(R.id.tv_distance);
        mTvArrivalPoint = view.findViewById(R.id.tv_arrival_point);
        mTvMoney = view.findViewById(R.id.tv_money);

    }
}
